import java.util.Arrays;

public class MySequentialList {
    //顺序表是一个数组
    public int[] array;
    public int usedSize;//表示整个数组的有效数据
    //构造方法
    public MySequentialList(){
        this.array = new int[10];
    }
    //打印顺序表
    public void display(){
        for (int i = 0; i < this.usedSize; i++) {
            System.out.print(this.array[i] + " ");
        }
        System.out.println();//换行
    }
    //获取顺序表的有效长度
    public int size(){
        return this.usedSize;
    }
    //在pos位置新增元素
    public void add(int pos,int data){
        if (pos<0||pos>this.usedSize){
            System.out.println("pos位置不合法");
            return;
        }
        dilatation();
        for (int i = this.usedSize-1; i >= pos; i--) {
            this.array[i+1] = this.array[i];
        }
        this.array[pos] = data;
        this.usedSize++;
    }
    //扩容
    public void dilatation(){
        if (this.array.length == this.usedSize){
            this.array = Arrays.copyOf(this.array,this.array.length*2);
        }

    }
    //判定是否包含某个元素
    public boolean contains(int toFind){
        for (int i = 0; i < this.usedSize; i++) {
            if (this.array[i] == toFind)
                return true;
        }
        return false;
    }
    //查找某个元素对应的位置
    public int search(int toFind){
        for (int i = 0; i < this.usedSize; i++) {
            if (this.array[i] == toFind)
                return i;
        }
        return -1;
    }
    //判断顺序表是否为空
    public boolean isEmpty(){
        return 0 == this.usedSize;
    }
    //把pos位置元素设为value
    public void setPos(int pos,int value){
        if (pos<0 || pos > this.usedSize){
            System.out.println("pos 位置不合法");
            return;
        }
        if (isEmpty()){
            System.out.println("顺序表为空");
            return;
        }
        this.array[pos] = value;
    }
    //删除第一次出现的关键字key
    public void remove(int toRemove){
        if (isEmpty())
            System.out.println("顺序表为空");
        int index = search(toRemove);
        if (index == -1){
            System.out.println("要删除的数字不存在");
            return;
        }
        for (int i = index; i < this.usedSize-1; i++) {
            this.array[i] = this.array[i+1];
        }
        this.usedSize--;
    }
    //清空顺序表
    public void clear(){
        this.usedSize = 0;
        //如果是引用类型
//        for (int i = 0; i < this.usedSize; i++) {
//            this.array[i] = null;
//        }
    }
}



























